package com.qq.BFMRSE.frame;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import com.qq.BFMRSE.util.BFConstants;
public class mainFrame extends JFrame implements ActionListener,MouseListener{
	private JPanel contentPane;
    private JMenuBar mbMain;
    private JMenu jMenu1;
    private JToolBar tobBar;
    private JButton btnToolAdd;
    private JButton btnToolUpdate;
    private JButton btnTooldec;
    private JButton btnToolSelect;
    private JButton btnToolKeyGen;
   // private JButton btnDec;
    private JTextField searchInput;
    private JButton btnSearch;
    private JLabel logoutLabel;
    private fileUploadFrame fileUpFrame;//文件上传索引
    private searchFrame searFrame;//文件检索
    private keyGenFrame kGenFrame;//秘钥生成
    private decFrame deFrame;//文件解密
    private updateFrame upFrame;//文件更新
    private String userName="";
    public mainFrame(String userName) {
    	this.userName=userName;
    	// setDefaultCloseOperation(EXIT_ON_CLOSE);
         init();
         this.setResizable(false);
         this.tobBar.setFloatable(false);
         ImageIcon imgAdd=new ImageIcon("images\\insert.png");
         ImageIcon imgUpdate=new ImageIcon("images\\update.png");
         ImageIcon imgDelete=new ImageIcon("images\\delete.png");
         ImageIcon imgSelect=new ImageIcon("images\\select.png");;
         ImageIcon imgUserSet=new ImageIcon("images\\users.png");
         ImageIcon imgDec=new ImageIcon("images\\desc.png");
         ImageIcon imgBF=new ImageIcon("images\\BFMRSE.png");
         this.setIconImage(imgBF.getImage()); 
         this.btnToolAdd.setIcon(imgAdd);
         this.btnToolUpdate.setIcon(imgUpdate);
         this.btnTooldec.setIcon(imgDelete);
         this.btnToolSelect.setIcon(imgSelect);
         this.btnToolKeyGen.setIcon(imgUserSet);
         this.btnTooldec.setIcon(imgDec);
      
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void init(){
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        
        mbMain = new JMenuBar();
        jMenu1 = new JMenu();
        tobBar = new JToolBar();
        btnToolAdd = new JButton();
        btnToolUpdate = new JButton();
        btnToolUpdate = new JButton();
        btnTooldec = new JButton();
        btnToolSelect = new JButton();
        btnToolKeyGen = new JButton();
        searchInput = new JTextField();
        btnSearch = new JButton("搜索");
        btnSearch.addActionListener(this);
        this.setJMenuBar(mbMain);
    	this.setSize(560, 300);
        this.setTitle("BFMRSE主界面");
        this.setLocationRelativeTo(null);
        jMenu1.setText("功能");
        tobBar.setBorder(BorderFactory.createEtchedBorder());
        tobBar.setBounds(new Rectangle(0, 0, 700, 40));
        btnToolAdd.setToolTipText("add");
        btnToolAdd.setText("上传文件集");
        btnToolAdd.addActionListener(this);
        btnToolUpdate.setToolTipText("update");
        btnToolUpdate.setText("更新文件");
        btnToolUpdate.addActionListener(this);
        btnToolSelect.setToolTipText("select");
        btnToolSelect.setText("检索文件");
        btnToolSelect.addActionListener(this);
        btnTooldec.setText("文件解密");
        btnTooldec.setToolTipText("decrypt");
        btnTooldec.addActionListener(this);
        btnToolKeyGen.setToolTipText("keyGen"); 
        btnToolKeyGen.setText("秘钥生成");
        btnToolKeyGen.addActionListener(this);
        mbMain.add(jMenu1);
        contentPane.add(tobBar);
      //  tobBar.addSeparator();
        tobBar.add(btnToolAdd);
        //tobBar.addSeparator();
        tobBar.add(btnToolSelect);
        tobBar.add(btnTooldec);
        //tobBar.addSeparator();
        tobBar.add(btnToolUpdate);
        //tobBar.addSeparator();
        tobBar.add(btnToolKeyGen);
        //tobBar.addSeparator();
        searchInput.setBounds(97, 105, 226, 30);
        searchInput.setColumns(10);
        contentPane.add(searchInput);
        btnSearch.setBounds(353, 105, 85, 30);
        contentPane.add(btnSearch);
        logoutLabel=new JLabel("用户注销");
        logoutLabel.setBounds(480, 200, 80, 50);
        logoutLabel.addMouseListener(this);
        contentPane.add(logoutLabel);
    }
    public static void main(String []args){
    	mainFrame ma=new mainFrame("yuan");
    	ma.setVisible(true);
    	ma.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(btnToolAdd)){
			fileUpFrame=new fileUploadFrame(userName);
			fileUpFrame.setVisible(true);
			fileUpFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		}else if(e.getSource().equals(btnToolSelect)){
			searFrame=new searchFrame(userName);
			searFrame.setVisible(true);
			searFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		}else if(e.getSource().equals(btnToolKeyGen)){
			kGenFrame=new keyGenFrame();
			kGenFrame.setVisible(true);
			kGenFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		}else if(e.getSource().equals(btnSearch)){
			String words=searchInput.getText();
			if("".equals(words)){
				JOptionPane.showMessageDialog(this, "请求的关键词为空！",
						"提示",JOptionPane.INFORMATION_MESSAGE);
			}else {
				searFrame=new searchFrame(userName);
				searFrame.getSearchInput().setText(words);
				searFrame.setVisible(true);
				searFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
			
		}else if(e.getSource()==btnTooldec){
			//进行解密操作
			deFrame=new decFrame();
			deFrame.setVisible(true);
			deFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		}else if(e.getSource()==btnToolUpdate){
			upFrame=new updateFrame(userName);
			upFrame.setVisible(true);
			upFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==logoutLabel){
			JOptionPane.showMessageDialog(this,  "用户"+userName+"将注销！",
					"提示",JOptionPane.INFORMATION_MESSAGE);
			this.dispose();
			BFConstants.INDEXPATH=BFConstants.INDEXPATH.substring(BFConstants.INDEXPATH.lastIndexOf(File.separator)+1);
			BFConstants.WORDINFOFILE=BFConstants.WORDINFOFILE.substring(BFConstants.WORDINFOFILE.lastIndexOf(File.separator)+1);
			loginFrame logFrame=new loginFrame();
			logFrame.setVisible(true);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
